package controllers

import (
	"encoding/json"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type InventoryController struct {
	BaseController
}

func (c *InventoryController) Prepare() {
	//先执行
	c.BaseController.Prepare()
}

//查询单条库存
func (c *InventoryController) ShowInventory() {
	/*
		pid, _ := c.GetInt64("pid")
		dep := c.curUser.DepartmentId
		dr := c.GetString("dr")
		am := c.GetString("am")

		md5str := services.CalculateEntryHas(map[string]interface{}{"PID": pid, "DEP": dep, "DR": dr, "AM": am})

		inv := services.CheckExistInventory(md5str)

		if inv.Id == 0 {
			c.jsonResult(enums.JRCodeFailed, "", "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "", inv)
		}*/
}

//重置信息
func (c *InventoryController) CheckEntry() {
	if err := services.ReCalculationHascode(); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "OK", "")
}

//查看库存
func (c *InventoryController) Inventory() {
	c.checkAuthor("inventory/inventory")

	c.GetInventoryProduct()

	//获取类别
	c.showErpTypeid()

	typeid := c.GetString("typeid", "10")

	c.Data["activeSidebarUrl"] = "/inventory/inventory"

	c.display("inventory/inventory_"+typeid+".html", "inventory/inventory_"+typeid+"_footerjs.html")
}

//销售获取库存产品
func (c *InventoryController) GetRetailInventoryProduct() {
	if c.Ctx.Input.IsPost() { //POST提交返回数据
		var params models.ErpInventoryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		params.NeedPrice = true
		params.Isnegative = "1"
		data, total := models.ErpInventoryPageList(&params)
		//获取价格
		paramsPrice := new(models.InventoryPriceQueryParams)
		for k, _ := range data {
			paramsPrice.DepId = data[k].DepartmentId
			paramsPrice.Snd = data[k].Snd
			paramsPrice.Product = data[k].Products
			paramsPrice.Sph = data[k].Snd.Sph
			paramsPrice.Cyl = data[k].Snd.Cyl
			paramsPrice.Add = data[k].Snd.Add
			price := models.GetInventoryPriceForQueryParams(paramsPrice);
			if price.Id > 0 {
				data[k].SuggestedPrice = price.SuggestedPrice
				data[k].PriceId = price.Id
			}
		}
		c.jsonDataTable(total, data)
	}
}

func (c *InventoryController) GetInventoryProduct() {
	if c.Ctx.Input.IsPost() { //POST提交返回数据
		var params models.ErpInventoryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		var total int64
		var data interface{}
		params.NeedPrice = false
		data, total = models.ErpInventoryPageList(&params)
		c.jsonDataTable(total, data)
	}
}

//查询产品库存信息
func (c *InventoryController) SelectInventory() {

	c.GetInventoryProduct()
	c.Data["query_keyword"] = c.GetString("query_keyword")
	c.Data["query_dr"] = c.GetString("query_dr")
	c.Data["query_am"] = c.GetString("query_am")

	c.display("", "inventory/selectinventory_footerjs.html", "shared/layout_dialog.html")
}

//下载库存统计表
func (c *InventoryController) ExcelInventory() {

	var params models.ErpInventoryQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	if url, err := services.ProductInventoryCreateExcel(&params); err != nil {
		c.jsonResult(enums.JRCodeFailed, "表格生成失败", "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "", url)
	}

	c.jsonResult(enums.JRCodeFailed, "表格生成失败", "")
}

//库存流水
func (c *InventoryController) Flowing() {
	c.checkAuthor("inventory/flowing")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpFlowingDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		data, total := models.ErpFlowingDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	c.Data["activeSidebarUrl"] = "/inventory/flowing"
	c.display("", "inventory/flowing_footerjs.html")
}
